import React, { Component } from 'react';
import RegisterForm from './components/RegisterForm';
import TermslInfo from './components/TermsInfo'
import { hashHistory } from 'react-router';
import { RegisterService } from '../../services'

export default class Register extends Component {
  static displayName = 'Register';

  constructor(props) {
    super(props);
    this.state = {
      agree : false
    };
  }

  onAgree = () =>{
    this.setState({ agree : true})
  }

  checkAccount = async(value) =>{
    let result = await RegisterService.checkCompanyAccount(value);
    if(!result){ return "账户名称重复" }
  }

  onSubmit = async(data) =>{
    let result = await RegisterService.register(data);
    setTimeout(() => { hashHistory.push('/register/complete') }, 200);
    return true
  }

  render() {
    if(!this.state.agree){
      return (
        <div className="register-page">
          <TermslInfo onAgree={this.onAgree}/>
        </div>
      )
    }else{
      return (
        <div className="register-page">
          <RegisterForm checkAccount={this.checkAccount} onSubmit={this.onSubmit}/>
        </div>
      );
    }
  }
}
